function animation(command, count){
	id1 = requestAnimationFrame(function(){
		animation(command,count);
	});
	if(ready == 'yes'){
		switch(command){
			case 'go':
					for(var i = 0; i < count; i++){
						switch(direction){
							case 0: 
								go(0,50,parseInt(canvas.style.top)-i+i*50);
								break;
							case 1: 
								go(1,50,parseInt(canvas.style.left)-i+i*50);
								break;
							case 2: 
								go(2,50,parseInt(canvas.style.top)-i+i*50);
								break;
							case 3: 
								go(3,50,parseInt(canvas.style.left)-i+i*50);
								break;
						}
					}	
				break;
			case 'build':
				var top = parseInt(canvas.style.top);
				var left = parseInt(canvas.style.left);
				switch(direction){
					case 0:
						if(top <= 50){
							alert('外面不能修墙');
							break;
						}
						if(map[top/50-2][left/50-1] == 0){
							alert('已经有墙了，不能再修了');
							break;
						}
						ctx1.fillStyle = '#49777e';
						ctx1.fillRect(left, top-50, 50, 50);
						map[top/50 -2][left/50 -1] = 0;
						break;
					case 1:
						if(left >= 500){
							alert('外面不能修墙');
							break;
						}
						if(map[top/50-1][left/50] == 0){
							alert('已经有墙了，不能再修了');
							break;
						}
						ctx1.fillStyle = '#49777e';
						ctx1.fillRect(left+50, top, 50, 50);
						map[top/50 -1][left/50] = 0;
						break;
					case 2:
						if(top >= 500){
							alert('外面不能修墙');
							break;
						}
						if(map[top/50][left/50-1] == 0){
							alert('已经有墙了，不能再修了');
							break;
						}
						ctx1.fillStyle = '#49777e';
						ctx1.fillRect(left, top+50, 50, 50);
						map[top/50][left/50 -1] = 0;
						break;
					case 3:
						if(left <= 50){
							alert('外面不能修墙');
							break;
						}
						if(map[top/50-1][left/50-2] == 0){
							alert('已经有墙了，不能再修了');
							break;
						}
						ctx1.fillStyle = '#49777e';
						ctx1.fillRect(left-50, top, 50, 50);
						map[top/50 -1][left/50-2] = 0;
						break;
				}
				break;
			case 'bru':
				var top = parseInt(canvas.style.top);
				var left = parseInt(canvas.style.left);
				switch(direction){
					case 0:
						if(top <= 50){
							alert('外面不能染色');
							break;
						}
						if(map[top/50-2][left/50-1] != 0){
							alert('没有墙的地方不能染色');
							break;
						}
						ctx1.fillStyle = count;
						ctx1.fillRect(left, top-50, 50, 50);
						break;
						break;
					case 1:
						if(left >= 500){
							alert('外面不能染色');
							break;
						}
						if(map[top/50-1][left/50] != 0){
							alert('没有墙的地方不能染色');
							break;
						}
						ctx1.fillStyle = count;
						ctx1.fillRect(left+50, top, 50, 50);
						break;
					case 2:
						if(top >= 500){
							alert('外面不能染色');
							break;
						}
						if(map[top/50][left/50-1] != 0){
							alert('没有墙的地方不能染色');
							break;
						}
						ctx1.fillStyle = count;
						ctx1.fillRect(left, top+50, 50, 50);
						break;
					case 3:
						if(left <= 50){
							alert('外面不能染色');
							break;
						}
						if(map[top/50-1][left/50-2] != 0){
							alert('没有墙的地方不能染色');
							break;
						}
						ctx1.fillStyle = count;
						ctx1.fillRect(left-50, top, 50, 50);
						break;
				}
				break;
			case 'tun lef': 
				
					rotate(-90);
					direction = (direction+3)%4;
				
				break;
			case 'tun rig': 
				
					rotate(90);
					direction = (direction+1)%4;
				
				break;
			case 'tun bac': 
				
					rotate(180);
					direction = (direction+2)%4;
				
				break;
			case 'tra lef':
				goLoop(count, 3);
				break;
			case 'tra top':
				goLoop(count, 0)
				break;
			case 'tra rig':
				goLoop(count, 1)
				break;
			case 'tra bot':
				goLoop(count, 2)
				break;
			case 'mov lef':
				
					direction = 3;
					rotate((270 - getAngle()%360));
					goLoop(count, 3)
				
				break;
			case 'mov top':
				
					direction = 0;
					rotate((0 - getAngle()));
					goLoop(count, 0)
				
				break;
			case 'mov rig':
				
					direction = 1;
					rotate((90 - getAngle()%360));
					goLoop(count, 1)
				
				break;
			case 'mov bot':
				
					direction = 2;
					rotate((180 - getAngle()%360));
					goLoop(count, 2)
				
				break;
		}
		cancelAnimationFrame(id1);
	}

}

function goLoop(number, direction){
	id2 = requestAnimationFrame(function(){
		goLoop(number, direction);
	});
	if(ready == 'yes'){
		for(var i = 0; i < number; i++){
			switch(direction){
				case 0: 
					go(0,50,parseInt(canvas.style.top)-i+i*50);
					break;
				case 1: 
					go(1,50,parseInt(canvas.style.left)-i+i*50);
					break;
				case 2: 
					go(2,50,parseInt(canvas.style.top)-i+i*50);
					break;
				case 3: 
					go(3,50,parseInt(canvas.style.left)-i+i*50);
					break;
			}
			
		}	
		cancelAnimationFrame(id2);
	}
}